import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:osc/common/enums.dart';
import 'package:osc/res/dimens.dart';
import 'package:osc/ui/widgets/small_widgets.dart';
import 'package:osc/utils/pd_util.dart';


///
/// 点击事件
///
typedef void OnClickEvent(int id);

class PublishDialog extends StatefulWidget {
  final OnClickEvent onClickEvent;
  const PublishDialog({Key key, @required this.onClickEvent}) : super(key: key);

  @override
  _PublishDialogState createState() => new _PublishDialogState();
}

class _PublishDialogState extends State<PublishDialog> with TickerProviderStateMixin<PublishDialog>{
  _PublishDialogState();


  AnimationController animationController;
  CurvedAnimation curve;
  Animation<double> animation;
  bool showingPopWindow = false;

  double screenHeight = 0;
  double screenWidth = 0;
  double iconWidth = Dimens.view_item_icon_height_bigger;
  double padding = Dimens.gap_view_small;

  int clickId = 0;

  @override
  void initState() {
    super.initState();

    animationController = AnimationController(
        duration: const Duration(milliseconds: 1500), vsync: this, animationBehavior: AnimationBehavior.preserve);
    curve = CurvedAnimation(parent: animationController, curve: Curves.linear);
    animation = Tween(begin: 0.0, end: 0.125).animate(animationController);

    animationController.animateTo(0.125);

    animationController.addListener((){
      if(showingPopWindow){

      }
      showingPopWindow = !showingPopWindow;
    });


  }

  @override
  void dispose() {
    animationController?.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {

    screenHeight = MediaQuery.of(context).size.height;
    screenWidth = MediaQuery.of(context).size.width;
    return Stack(
      children: <Widget>[
        Positioned(
          bottom: 0,
            left: screenWidth/2-iconWidth/2-padding*2,
            child: Padding(
              padding: EdgeInsets.all(Dimens.gap_view_small),
              child: buildClose(PublishItemId.ID_CLOSE),
            ),
        ),
        buildPublishItem("发猿圈", "ic_new_tweet", screenWidth/4-iconWidth/2-padding, screenHeight-160, PublishItemId.ID_TWEET, Colors.indigo),
        buildPublishItem("发问答", "ic_new_question", screenWidth/2-iconWidth/2-padding, screenHeight-190, PublishItemId.ID_QUESTION, Colors.blue),
        buildPublishItem("投递文章", "ic_new_article", screenWidth*3/4-iconWidth/2-padding, screenHeight-160, PublishItemId.ID_ARTICLE, Colors.orangeAccent),
      ],
    );
  }

  buildClose(int clickId){
    return GestureDetector(
      onTap: (){
        if(widget.onClickEvent != null){
          widget.onClickEvent(clickId);
        }
        setState(() {
          if(showingPopWindow){
            animationController.animateTo(0);
          } else {
            animationController.animateTo(0.125);
          }
        });
      },
      child: RotationTransition(
          turns: curve,
          child: Container(
            decoration: BoxDecoration(
              color: Colors.green,
              shape: BoxShape.circle,
            ),
            child: Padding(
              padding: EdgeInsets.all(Dimens.gap_view_small),
              child: Image.asset(
                PdUtil.getImagePath("icon_add_orange"),
                height: iconWidth,
              ),
            ),
          ),
      ),
    );
  }


  buildPublishItem(String title, String imageName, double toX, double toY, int clickId, Color bg){
    return Transform.translate(
        offset: Offset(toX, toY),
        child: Column(
          children: <Widget>[
            Container(
              padding: EdgeInsets.symmetric(vertical: Dimens.gap_view_smaller, horizontal: Dimens.gap_view_smaller),
              decoration: BoxDecoration(
                color: Colors.black,
                borderRadius: BorderRadius.all(Radius.circular(Dimens.view_radius_size_smaller)),
              ),
              child: SmallWidgets.buildText(title, Colors.white, Dimens.font_sp12),
            ),
            Container(
              margin: EdgeInsets.only(top: Dimens.gap_view_small),
              decoration: BoxDecoration(
                color: bg,
                shape: BoxShape.circle,
              ),
              child: Padding(
                padding: EdgeInsets.zero,
                child: GestureDetector(
                  onTap: (){
                    if(widget.onClickEvent != null){
                      widget.onClickEvent(clickId);
                    }
                    setState(() {
                      if(showingPopWindow){
                        animationController.animateTo(0);
                      } else {
                        animationController.animateTo(0.125);
                      }
                    });
                  },
                  child: Padding(
                    padding: EdgeInsets.all(Dimens.gap_view_small),
                    child: Image.asset(
                      PdUtil.getImagePath(imageName),
                      height: iconWidth,
                    ),
                  ),
                ),
              ),
            )
          ],
        )
    );
  }
}

